﻿using System;
using System.Text;

namespace ReverseAverageLinearEquation
{
    class ReverseAverageLinearEquation
    {
        static string ReverseNumber(string number)
        {
            StringBuilder num = new StringBuilder(number);
            StringBuilder result = new StringBuilder();
            bool isNegative = number[0] == '-';
            int leadingZeroes = 0;

            if (isNegative)
            {
                num.Remove(0, 1);
            }

            for (int index = 0, length = num.Length - 1; index < length; index++) //if the number is 00001 => 1
            {
                if (num[index] == '0')
                {
                    leadingZeroes++;
                }
                else
                {
                    break;
                }
            }

            if (isNegative)
            {
                result.Append('-');
            }

            for (int index = num.Length - 1; index >= leadingZeroes; index--) //00000954 => 459
            {
                result.Append(num[index]);
            }

            return result.ToString();
        }

        static double Average(int[] array)
        {
            int sum = 0;
            for (int i = 0, length = array.Length; i < length; i++)
            {
                sum += array[i];
            }
            return (double)sum / array.Length;
        }

        static double CalculateEquation(int a, int b)
        {
            return -b / (double)a;
        }

        static void InputAverage()
        {
            Console.Write("Enter array size and numbers: ");
            int[] array = new int[int.Parse(Console.ReadLine())];

            for (int index = 0, length = array.Length; index < length; index++)
            {
                Console.Write("[{0}] = ", index);
                array[index] = int.Parse(Console.ReadLine());
            }

            if (array.Length > 0)
            {
                Console.WriteLine("Average: {0}", Average(array));
            }
            else
            {
                Console.WriteLine("Array should have elements.");
            }
        }

        static void InputEquation()
        {
            Console.Write("Enter a: ");
            int a = int.Parse(Console.ReadLine());
            Console.Write("Enter b: ");
            int b = int.Parse(Console.ReadLine());

            if (a != 0)
            {
                Console.WriteLine("CalculateEquation: {0}", CalculateEquation(a, b));
            }
            else
            {
                Console.WriteLine("Coefficient 'a' should not be zero.");
            }
        }

        static void InputReverseNumber()
        {
            Console.Write("Enter a number greater than 0: ");
            string number = Console.ReadLine();
            double n = double.Parse(number);

            if (n >= 0)
            {
                Console.WriteLine("ReverseNumber: {0}", ReverseNumber(number));
            }
            else
            {
                Console.WriteLine("The number should be non-negative.");
            }
        }

        static void Main()
        {
            Console.WriteLine("0: ReverseNumber\n1: Average\n2: CalculateEquation");
            
            switch (Console.ReadLine())
            {
                case "0": InputReverseNumber(); break;
                case "1": InputAverage(); break;
                case "2": InputEquation(); break;
                default: break;
            }
        }
    }
}